
local nmgsslx = require "nmgssl"

function nmgsslx.reqjson(urlStr)
	local jsonStr = nmgsslx.requrl(urlStr)
	local st = string.find(jsonStr,"{")
	local tempStr = string.reverse(jsonStr)
	local ed = string.find(tempStr,"}")
	if st==1 and ed==1 then
		return jsonStr
	end
	if (not st) or (not ed) then
		return jsonStr
	end
	ed = #jsonStr - ed + 1
	return string.sub(jsonStr,st,ed)
end

function nmgsslx.requrl(ourlStr)
	local profix = "https://"
	if string.sub(ourlStr,1,#profix) ~= profix then
		error("url error")
	end
	local urlStr = string.sub(ourlStr,(#profix)+1)
	local pathIndex = string.find(urlStr,"/")
	if pathIndex > 0 then
		pathStr = string.sub(urlStr,pathIndex)
		urlStr = string.sub(urlStr,1,pathIndex-1)
		-- momo的path必须完整的url
		local header = string.format("GET %s \r\n\r\n",ourlStr)
		return nmgsslx.req(urlStr,nil,header)
	else
		return nmgsslx.req(ourlStr)
	end
end

function nmgsslx.req(host,port,header)
	assert(type(host)=="string")
	port = port or 443
	assert(type(port)=="number")
	header = header or "GET /\r\n\r\n"
	assert(type(header)=="string")
	return nmgsslx.request1(host,port,header)
end


return nmgsslx